/**
 * 用户的相关操作
 * 周捷
 * 2013-12-25
 */
package com.passkeeper.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.passkeeper.jdbc.model.User;
import com.passkeeper.jdbc.service.inteface.IUserService;
import com.passkeeper.util.UtilFunction;

@SuppressWarnings("serial")
@Component("userAction") 
@Scope("prototype")
@Namespace("/user")
@Results({
	@Result(name = "homepage", location = "/homepage.jsp" , type="redirect"),
	@Result(name = "login", location = "/login.jsp"),
	@Result(name = "error", location = "/util/error.jsp") })
public class UserAction extends ActionSupport {
	@Autowired
	private IUserService userService;

	@Action("login")
	public String login() {

		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			String username = UtilFunction.doNull(request.getParameter("pk_username"));
			String password = UtilFunction.doNull(request.getParameter("pk_password"));
			password = UtilFunction.MD5(password);
			User user = userService.findUserByUsername(username);
			if(user.getPassword()!=null && user.getPassword().equals(password)){
				request.getSession().setAttribute("userinfo", user);
			}else{
				request.setAttribute("loginfo", "用户不存在或密码不正确");
				return "login";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("errorMsg", e.getMessage());
			return "error";
		}
		
		return "homepage";
	}
}
